package com.rytx.system.service.impl;

import java.util.*;

import cn.hutool.core.util.ArrayUtil;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.utils.StringUtils;
import com.rytx.system.domain.ysp.*;
import com.rytx.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rytx.system.service.IArtRentalService;


/**
 * 艺术品Service业务层处理
 *
 * @author rytx
 * @date 2024-05-13
 */
@Service
public class ArtRentalServiceImpl implements IArtRentalService
{
    private static Map<Long, Long> viewHistoryMap = new HashMap<>();
    private static Map<Long, Long> favoriteMap = new HashMap<>();

    @Autowired
    private ArtRentalMapper artRentalMapper;
    @Autowired
    private PCategoryMapper pCategoryMapper;
    @Autowired
    private PPlacementMapper pPlacementMapper;
    @Autowired
    private PStyleMapper pStyleMapper;
    @Autowired
    private PThemeMapper pThemeMapper;

    /**
     * 查询艺术品
     *
     * @param id 艺术品主键
     * @return 艺术品
     */
    @Override
//    @Cached(name = "artRental:", key = "#id", expire = 3600)
    public ArtRental selectArtRentalById(Long id)
    {
        return artRentalMapper.selectArtRentalById(id);
    }

    /**
     * 查询艺术品列表
     *
     * @param artRental 艺术品
     * @return 艺术品
     */
    @Override
//    @Cached(name = "artRentalList:", key = "#artRental.toString()", expire = 3600)
    public List<ArtRental> selectArtRentalList(ArtRental artRental)
    {
        return artRentalMapper.selectArtRentalList(artRental);
    }

    @Override
//    @Cached(name = "artRentalListByPro:", key = "#artRental.toString()", expire = 3600)
    public List<ArtRental> selectArtRentalListByPro(ArtRental artRental) {
        return artRentalMapper.selectArtRentalListByPro(artRental);
    }

    /**
     * 新增艺术品
     *
     * @param artRental 艺术品
     * @return 结果
     */
    @Override
//    @CacheInvalidate(name = "artRentalList:", key = "#artRental.toString()")
//    @CacheInvalidate(name = "artRentalListByPro:", key = "#artRental.toString()")
    public int insertArtRental(ArtRental artRental)
    {
        int i1 = artRentalMapper.insertArtRental(artRental);

        Long[] c = artRental.getcIds();
        Long[] p = artRental.getpIds();
        Long[] t = artRental.gettIds();
        Long[] s = artRental.getsIds();

        if (StringUtils.isNotEmpty(c)) {
            List<PCategory> pcList = new ArrayList<>();
            for(Long i : c)pcList.add(new PCategory(i, artRental.getId(),"0"));
            int pc = pCategoryMapper.insertBatchPCategory(pcList);
            artRental.setStrCids(ArrayUtil.join(c,","));
        }
        if (StringUtils.isNotEmpty(p)) {
            List<PPlacement> ppList = new ArrayList<>();
            for(Long i : p)ppList.add(new PPlacement(i, artRental.getId(),"0"));
            int pp = pPlacementMapper.insertBatchPPlacement(ppList);
            artRental.setStrPids(ArrayUtil.join(p,","));
        }
        if (StringUtils.isNotEmpty(t)) {
            List<PTheme> ptList = new ArrayList<>();
            for(Long i : t)ptList.add(new PTheme(i, artRental.getId(),"0"));
            int pt = pThemeMapper.insertBatchPTheme(ptList);
            artRental.setStrTids(ArrayUtil.join(t,","));
        }
        if (StringUtils.isNotEmpty(s)) {
            List<PStyle> psList = new ArrayList<>();
            for(Long i : s) psList.add(new PStyle(i, artRental.getId(),"0"));
            int ps = pStyleMapper.insertBatchPStyle(psList);
            artRental.setStrSids(ArrayUtil.join(s,","));
        }
        return i1;
    }

    /**
     * 修改艺术品
     *
     * @param artRental 艺术品
     * @return 结果
     */
    @Override
//    @CacheUpdate(name = "artRental:", key = "#artRental.id", value = "#artRental")
//    @CacheInvalidate(name = "artRentalList:", key = "#artRental.toString()")
//    @CacheInvalidate(name = "artRentalListByPro:", key = "#artRental.toString()")
    public int updateArtRental(ArtRental artRental)
    {
        Long[] c = artRental.getcIds();
        Long[] p = artRental.getpIds();
        Long[] t = artRental.gettIds();
        Long[] s = artRental.getsIds();

        if (StringUtils.isNotEmpty(c)) {
            pCategoryMapper.deletePCategoryByRId(artRental.getId());
            List<PCategory> pcList = new ArrayList<>();
            for(Long i : c)pcList.add(new PCategory(i, artRental.getId(),"0"));
            int pc = pCategoryMapper.insertBatchPCategory(pcList);
            artRental.setStrCids(ArrayUtil.join(c,","));
        }
        if (StringUtils.isNotEmpty(p)) {
            pPlacementMapper.deletePPlacementByRId(artRental.getId());
            List<PPlacement> ppList = new ArrayList<>();
            for(Long i : p)ppList.add(new PPlacement(i, artRental.getId(),"0"));
            int pp = pPlacementMapper.insertBatchPPlacement(ppList);
            artRental.setStrPids(ArrayUtil.join(p,","));
        }
        if (StringUtils.isNotEmpty(t)) {
            pThemeMapper.deletePThemeByRId(artRental.getId());
            List<PTheme> ptList = new ArrayList<>();
            for(Long i : t)ptList.add(new PTheme(i, artRental.getId(),"0"));
            int pt = pThemeMapper.insertBatchPTheme(ptList);
            artRental.setStrTids(ArrayUtil.join(t,","));
        }
        if (StringUtils.isNotEmpty(s)) {
            pStyleMapper.deletePStyleByRId(artRental.getId());
            List<PStyle> psList = new ArrayList<>();
            for(Long i : s) psList.add(new PStyle(i, artRental.getId(),"0"));
            int ps = pStyleMapper.insertBatchPStyle(psList);
            artRental.setStrSids(ArrayUtil.join(s,","));
        }
        return artRentalMapper.updateArtRental(artRental);
    }

    /**
     * 批量删除艺术品
     *
     * @param ids 需要删除的艺术品主键
     * @return 结果
     */
    @Override
//    @CacheInvalidate(name = "artRentalList:", key = "#ids.toString()")
//    @CacheInvalidate(name = "artRentalListByPro:", key = "#ids.toString()")
    public int deleteArtRentalByIds(Long[] ids)
    {
        return artRentalMapper.deleteArtRentalByIds(ids);
    }

    /**
     * 删除艺术品信息
     *
     * @param id 艺术品主键
     * @return 结果
     */
    @Override
//    @CacheInvalidate(name = "artRental:", key = "#id")
//    @CacheInvalidate(name = "artRentalList:", key = "#id.toString()")
//    @CacheInvalidate(name = "artRentalListByPro:", key = "#id.toString()")
    public int deleteArtRentalById(Long id)
    {
        return artRentalMapper.deleteArtRentalById(id);
    }


    @Override
    public void addViewHistoryTask() {
        viewHistoryMap.forEach((k, v) -> {
            ArtRental artRental = new ArtRental();
            artRental.setId(k);
            artRental.setArtView(v);
            artRentalMapper.updateArtRentalViewHistory(artRental);
        });
        viewHistoryMap.clear();
    }

    @Override
    public AjaxResult addFavorite(Long id, Long num) {
        Long count = favoriteMap.getOrDefault(id, 0l);
        favoriteMap.put(id, count + num);
        return AjaxResult.success();
    }

    @Override
    public void addFavoriteTask() {
        favoriteMap.forEach((k, v) -> {
            ArtRental artRental = new ArtRental();
            artRental.setId(k);
            artRental.setArtColl(v);
            artRentalMapper.updateArtRentalViewHistory(artRental);
        });
        favoriteMap.clear();
    }

    @Override
    public List<ArtRental> selectArtRentalByIds(String cartRentalIds) {
        return artRentalMapper.getArtRentalByIds(cartRentalIds);
    }



    @Override
    public AjaxResult addViewHistory(Long id) {
        Long count = viewHistoryMap.getOrDefault(id, 0l);
        viewHistoryMap.put(id, count + 1l);
        return AjaxResult.success();
    }
    /**
     * 设置商品折扣
     */
    @Override
    public void setshopdiscount(ArtRental artRental) {
        artRentalMapper.setshopdiscount(artRental);
    }

    @Override
    public void setspecial(ArtRental artRental) {
        artRentalMapper.setspecial(artRental);

    }
}
